import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import '../common.dart';

import '../kong.dart' if (dart.library.io) '../im/im_manager.dart';

class LoginType extends StatefulWidget {
  LoginType({super.key});

  @override
  State<LoginType> createState() => _SettingPageState();
}

class _SettingPageState extends State<LoginType> {
  // class LoginType extends StatelessWidget {
  final RxBool _isLogin = true.obs;
  bool _obscureText = true;
  final FocusNode _focusNode = FocusNode();
  RxInt codetext = 60.obs;
  RxBool butdisabled = false.obs;

  RxBool valgroup = true.obs;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  final TextEditingController phone = TextEditingController(
    text: myget('phone'),
  );
  final TextEditingController password = TextEditingController(
    text: myget('pws'),
  );

  final TextEditingController code = TextEditingController();

  @override
  void dispose() {
    code.dispose();
    phone.dispose();
    password.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    con(context);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(backgroundColor: Colors.black12),
      extendBodyBehindAppBar: true,
      body: GestureDetector(
        onTap: () {
          FocusScope.of(context).unfocus();
        },
        child: Column(
          children: [
            SafeArea(
              top: false,
              child: Image.asset(
                'static/images/dengli.png',
                fit: BoxFit.fitWidth,
              ),
            ),
            Container(
              height: 30,
              transform: Matrix4.translationValues(0.0, -15.0, 0.0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(30),
                color: Color(0XFFFFFFFF),
              ),
            ),
            Expanded(
              child: ListView(
                children: [
                  Column(
                    children: [
                      Container(
                        transform: Matrix4.translationValues(0.0, -40.0, 0.0),
                        child: GestureDetector(
                          onTap: () {
                            toNamed('/login/splash_screen');
                          },
                          child: Image.asset(
                            'static/images/open.png',
                            fit: BoxFit.fitWidth,
                            width: 160,
                          ),
                        ),
                      ),
                      // Obx(
                      //   () => _isLogin.value
                      //       ?
                      Container(
                        margin: EdgeInsets.symmetric(
                          horizontal: 30,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(color: Color(0XFFDDDDDD)),
                          ),
                        ),
                        child: Row(
                          children: [
                            SizedBox(width: 15),
                            Image.asset(
                              'static/images/guoqi.png',
                              fit: BoxFit.fitWidth,
                              width: 30,
                            ),
                            SizedBox(width: 5),
                            Text('+86', style: TextStyle(fontSize: 14)),
                            Expanded(
                              child: TextField(
                                focusNode: _focusNode,
                                controller: phone,
                                style: TextStyle(fontSize: 14),
                                decoration: InputDecoration(
                                  isCollapsed: true, //设置高度
                                  contentPadding: EdgeInsets.all(15),
                                  hintStyle: TextStyle(
                                    color: Color(0xff999999),
                                  ),
                                  border: InputBorder.none, // 隐藏输入框默认边框
                                  hintText: '请输入手机号', // 提示文本
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      // : Container(
                      //     margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                      //     decoration: BoxDecoration(
                      //         border: Border(
                      //             bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                      //     child: TextField(
                      //       style: TextStyle(fontSize: 14),
                      //       decoration: InputDecoration(
                      //         isCollapsed: true, //设置高度
                      //         contentPadding: EdgeInsets.all(15),
                      //         hintStyle: TextStyle(color: Color(0xff999999)),
                      //         border: InputBorder.none, // 隐藏输入框默认边框
                      //         hintText: '请输入电子邮箱地址', // 提示文本
                      //       ),
                      //     )),
                      // ),
                      Obx(
                        () =>
                            _isLogin.value
                                ? Container(
                                  margin: EdgeInsets.symmetric(horizontal: 30),
                                  decoration: BoxDecoration(
                                    border: Border(
                                      bottom: BorderSide(
                                        color: Color(0XFFDDDDDD),
                                      ),
                                    ),
                                  ),
                                  child: TextField(
                                    obscureText: true,
                                    controller: password,
                                    style: TextStyle(fontSize: 14),
                                    decoration: InputDecoration(
                                      // suffixIcon: IconButton(
                                      //   // 使用IconButton作为后缀图标按钮
                                      //   icon: Icon(
                                      //     size: 15,
                                      //     _obscureText
                                      //         ? Icons.visibility_off
                                      //         : Icons.visibility, // 根据_obscureText的值切换图标
                                      //   ),
                                      //   onPressed: () {}, // 点击图标时调用_toggleVisibility方法
                                      // ),
                                      isCollapsed: true, //设置高度
                                      contentPadding: EdgeInsets.all(15),
                                      hintStyle: TextStyle(
                                        color: Color(0xff999999),
                                      ),
                                      border: InputBorder.none, // 隐藏输入框默认边框
                                      hintText: '请输入密码', // 提示文本
                                    ),
                                  ),
                                )
                                : Container(
                                  margin: EdgeInsets.symmetric(
                                    horizontal: 30,
                                    vertical: 10,
                                  ),
                                  decoration: BoxDecoration(
                                    border: Border(
                                      bottom: BorderSide(
                                        color: Color(0XFFDDDDDD),
                                      ),
                                    ),
                                  ),
                                  child: Row(
                                    children: [
                                      Expanded(
                                        child: TextField(
                                          controller: code,
                                          style: TextStyle(fontSize: 14),
                                          decoration: InputDecoration(
                                            isCollapsed: true, //设置高度
                                            contentPadding: EdgeInsets.all(15),
                                            hintStyle: TextStyle(
                                              color: Color(0xff999999),
                                            ),
                                            border:
                                                InputBorder.none, // 隐藏输入框默认边框
                                            hintText:
                                                '请输入验证码 (新用户登录即注册)', // 提示文本
                                          ),
                                        ),
                                      ),
                                      GestureDetector(
                                        onTap: () {
                                          if (phone.text.isNotEmpty) {
                                            if (butdisabled.value) {
                                              return;
                                            }
                                            postData(
                                              url: '/phone-code',
                                              data: {
                                                "phone": phone.text,
                                                "value": "login",
                                              },
                                            ).then((res) {
                                              if (res != null) {
                                                showTost('发送成功');
                                                butdisabled.value = true;
                                                codetext.value = 60;

                                                Timer.periodic(
                                                  Duration(seconds: 1),
                                                  (timer) {
                                                    codetext.value--;
                                                    if (codetext.value < 1) {
                                                      codetext.value = 1;
                                                      timer.cancel();
                                                      butdisabled.value = false;
                                                    }

                                                    // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                                                  },
                                                );
                                              }
                                            });
                                          } else {
                                            showTost('输入手机号');
                                          }
                                        },
                                        child: Obx(
                                          () =>
                                              butdisabled.value
                                                  ? Text(
                                                    '已发送$codetext',
                                                    style: TextStyle(
                                                      fontSize: 12,
                                                      color: Color(0XFF0A72D9),
                                                    ),
                                                  )
                                                  : Text(
                                                    '发送验证码',
                                                    style: TextStyle(
                                                      fontSize: 12,
                                                      color: Color(0XFF0A72D9),
                                                    ),
                                                  ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                      ),

                      Container(
                        margin: EdgeInsets.only(right: 30, top: 15, bottom: 30),
                        alignment: Alignment.centerRight,
                        child: GestureDetector(
                          onTap: () {
                            _isLogin.value = !_isLogin.value;
                            code.text = '';
                            password.text = '';
                          },
                          child: Obx(
                            () =>
                                _isLogin.value
                                    ? Text(
                                      '验证码登录',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF0A72D9),
                                      ),
                                    )
                                    : Text(
                                      '密码登录',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF0A72D9),
                                      ),
                                    ),
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: () async {
                          if (!phone.text.isNotEmpty) {
                            showTost('输入手机号');
                            return;
                          }
                          if (_isLogin.value) {
                            if (!password.text.isNotEmpty) {
                              showTost('输入密码');
                              return;
                            }
                          } else {
                            if (!code.text.isNotEmpty) {
                              showTost('请输入验证码');
                              return;
                            }
                          }

                          loading();
                          postData(
                            url: '/login',
                            data: {
                              'phone': phone.text,
                              'password': password.text,
                              'phone_code': code.text,
                            },
                            load: false,
                          ).then((res) {
                            if (res != null) {
                              myset('token', res['token']);
                              myset('phone', phone.text);
                              myset('pws', password.text);

                              getData(url: '/userInfo', load: false).then((
                                res,
                              ) {
                                myset('user', res);

                                // getData(url: '/tim/getSin',load: false).then((ress){
                                // if(ress!=null){

                                // if(kIsWeb){
                                Get.offNamed('/');
                                // Get.offAllNamed('/',arguments: {'preventBack': true});
                                // }else{

                                //     ImManager().login(ress['sig'], ress['UserID'],res['name'],faceUrl:res['headimg']).then((val){
                                //       if(val){
                                //         Get.offAllNamed('/',arguments: {'preventBack': true});
                                //       }else{
                                //        showTost('IM登录失败');
                                //       }
                                //     });

                                // }
                                //  }
                                // });
                              });
                            } else {
                              Get.back();
                            }
                          });
                        },
                        child: Container(
                          width: 300,
                          height: 50,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular((50)),
                            gradient: LinearGradient(
                              colors: [
                                Color(0xff2189E7),
                                Color(0xffBF67F8),
                              ], // 定义颜色列表
                              begin: Alignment.topLeft, // 渐变开始的位置
                              end: Alignment.bottomRight, // 渐变结束的位置
                            ),
                          ),
                          child: Text(
                            '登 录',
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0XFFFFFFFF),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          GestureDetector(
                            onTap: () {
                              toNamed('/login/register_tow');
                            },
                            child: Text(
                              '注册新用户',
                              style: TextStyle(
                                fontSize: 12,
                                color: Color(0XFF999999),
                              ),
                            ),
                          ),
                          GestureDetector(
                            onTap: () {
                              toNamed('/login/get_password');
                            },
                            child: Text(
                              '忘记密码？',
                              style: TextStyle(
                                fontSize: 12,
                                color: Color(0XFF999999),
                              ),
                            ),
                          ),
                        ],
                      ),
                      // Container(
                      //   margin: EdgeInsets.only(left: 100, right: 100, top: 50),
                      //   child: Row(
                      //     mainAxisAlignment: MainAxisAlignment.spaceAround,
                      //     children: [
                      //       Image.asset(
                      //         'static/images/weixin.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //       Image.asset(
                      //         'static/images/pingguo.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //     ],
                      //   ),
                      // ),
                    ],
                  ),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Container(
                    width: 12,
                    height: 12,
                    margin: EdgeInsets.only(right: 3),
                    child: Obx(
                      () => Image.asset(
                        valgroup.value
                            ? 'static/web/shous.png'
                            : 'static/web/shou.png',
                        width: 12,
                        height: 12,
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Text('已阅读并同意', style: TextStyle(fontSize: 11)),
                ),
                GestureDetector(
                  onTap: () {
                    toNamed('/agreement?type=1');
                  },
                  child: Text(
                    '《用户服务协议》',
                    style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9)),
                  ),
                ),
                Text('与', style: TextStyle(fontSize: 11)),
                GestureDetector(
                  onTap: () {
                    toNamed('/agreement?type=2');
                  },
                  child: Text(
                    '《隐私权政策》',
                    style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9)),
                  ),
                ),
              ],
            ),
            SizedBox(height: 15),
          ],
        ),
      ),
    );
  }
}
